﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.PhongCat
{
    public partial class Cat_DanhSachLenhPopup : Form
    {
        public Cat_DanhSachLenhPopup()
        {
            InitializeComponent();
            Loaddata();
            SetPropertiesGrid();
            //SetPropertiesColumns();
        }

        public void Loaddata()
        {
            gridLenh.DataSource = DataAll.SelectAll("LenhSanXuat inner join PhongMau_SanPham On LenhSanXuat.IdSanPham = PhongMau_SanPham.IdSanPham",
                "LenhSanXuat.[ID],PhongMau_SanPham.[MaHang] as MaSanPham,LenhSanXuat.[IdSanPham],[NgayLapLenh],[NguoiLapLenh],LenhSanXuat.[SoLuong],[NgayGuiLenh],[NgaySuaLenh],[NgayDuyetLenh],LenhSanXuat.[TrangThai],[NgayChungTu],[NgayHoachToan],LenhSanXuat.[SoChungTu],LenhSanXuat.[GhiChu],[KhoDen],[KhoDi],[SXTuNgay],[SXDenNgay]",
                "WHERE LenhSanXuat.TrangThai = " + Convert.ToInt32(TrangThai.LenhSanXuat_CapNhatKho));
        }

        public void SetPropertiesGrid()
        {
            gvLenh.BestFitColumns();
            gvLenh.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvLenh.OptionsBehavior.Editable = false;
            gvLenh.OptionsSelection.MultiSelect = true;
            gvLenh.OptionsView.ShowGroupPanel = false;
            gvLenh.OptionsView.ShowFooter = true;
            gvLenh.OptionsFind.AlwaysVisible = true;
            gvLenh.Columns["ID"].Visible = false;
        }

        private void gvLenh_DoubleClick(object sender, EventArgs e)
        {
            TaoSanPhamCat.IdLenh = gvLenh.Columns["ID"].View.GetRowCellDisplayText(gvLenh.FocusedRowHandle, "ID");
            var lss = new TaoSanPhamCat();
            lss.ShowDialog();
            Loaddata();
            TaoSanPhamCat.IdLenh = "0";
        }

        private void gvLenh_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "SXDenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 < songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }

    }
}
